package org.vt.ece3574.pictoslider;

import org.vt.ece3574.pictoslider.facebook.FacebookManager;
import org.vt.ece3574.pictoslider.menus.HighScoreScreen;
import org.vt.ece3574.pictoslider.menus.Themes;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Pop Up Window which appears after a puzzle has been solved.
 * 
 * @author Michael
 */
/**
 * @author jscar89
 * 
 */
public class CongratulationsPopUpWindow extends Activity {

	/**
	 * Pop-up text which appears when someone clicks the post to facebook button
	 * when someone has already posted this high score to facebook.
	 */
	private static final String ALREADY_POSTED_TEXT = "You have already posted this to Facebook";
	/**
	 * The facebook manager for this activity. Use it to post to facebook and
	 * recieve callbacks when the post to facebook was successfull.
	 */
	private FacebookManager fbManager = new FacebookManager(this);
	/**
	 * True if the score has been posted to facebook. False if not.
	 */
	private boolean shared = false;
	/**
	 * Text which appears by default underneath "Congratulations" at the top of
	 * this activity. This text comes before the high score time.
	 */
	private static final String CONGRATS_MESSAGE_PART1 = "You solved the ";
	/**
	 * Text which appears by default underneath "Congratulations" at the top of
	 * this activity. This text comes after the puzzle difficulty.
	 */
	private static final String CONGRATS_MESSAGE_PART2 = " difficulty puzzle in ";
	/**
	 * Text which appears by default underneath "Congratulations" at the top of
	 * this activity. This text comes after the high score time.
	 */
	private static final String CONGRATS_MESSAGE_PART3 = " using only ";
	/**
	 * Text which appears by default underneath "Congratulations" at the top of
	 * this activity. This text comes after the number of swaps.
	 */
	private static final String CONGRATS_MESSAGE_PART4 = " moves!";

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle state) {
		super.onCreate(state);
		Themes.onActivityCreateSetTheme(this);
		Themes.onActivityCreateSetContentView(this, R.layout.congrats_popup);

		// Because this activity was started for a result, let the calling
		// activity know that result is OK.
		setResult(Activity.RESULT_OK, new Intent());

		// Put congratulations text into the 3 textviews on the screen
		TextView tv = (TextView) findViewById(R.id.textView1);
		tv.setTextColor(Themes.textColor(this));

		tv = (TextView) findViewById(R.id.textView2);

		Bundle b = getIntent().getExtras();
		long time = b.getLong(GameScreen.SOLVE_TIME);
		int moves = b.getInt(GameScreen.NUM_SWAPS);
		String difficulty = b.getString(GameScreen.DIFFICULTY);

		HighScoreObject hsObject = new HighScoreObject(0, time, null);
		tv.setText(CONGRATS_MESSAGE_PART1 + difficulty);
		tv.setTextColor(Themes.textColor(this));

		tv = (TextView) findViewById(R.id.textView3);
		tv.setText(CONGRATS_MESSAGE_PART2 + hsObject.getTimeAsString());
		tv.setTextColor(Themes.textColor(this));

		tv = (TextView) findViewById(R.id.textView4);
		tv.setText(CONGRATS_MESSAGE_PART3 + moves + CONGRATS_MESSAGE_PART4);
		tv.setTextColor(Themes.textColor(this));
	}

	/**
	 * OnClickListener for the Play Again button. Starts a new game with the
	 * same settings as the game just finished.
	 * 
	 * @param v
	 *            - The view which was clicked.
	 */
	public void beginNewGame(View v) {
		this.finish();
		startActivity(new Intent(this, GameScreen.class));
	}

	/**
	 * OnClickListener for the Post to Facebook button.
	 * 
	 * @param v
	 *            - The view which was clicked.
	 */
	public void postToFacebook(View v) {
		if (isOnline()) {
			if (!shared) {
				shared = true;
				// Disable change of screen orientation during posting action
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
				fbManager.post();
			} else {
				Toast t = Toast.makeText(getApplicationContext(),
						ALREADY_POSTED_TEXT, Toast.LENGTH_SHORT);
				t.show();
			}
		} else {
			Toast.makeText(
					getApplicationContext(),
					"You must be connected to the Internet to post to Facebook.",
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * OnClickListener for the View High Scores Button.
	 * 
	 * @param v
	 *            - The view which was clicked.
	 */
	public void goToHighScoresScreen(View v) {
		this.finish();
		startActivity(new Intent(this, HighScoreScreen.class));
	}

	/**
	 * OnClickListener for the Quit button. Returns user to the main screen.
	 * 
	 * @param v
	 *            - The view which was clicked.
	 */
	public void quitGame(View v) {
		this.finish();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		fbManager.authorizeCallback(requestCode, resultCode, data);
	}

	/**
	 * This method is called by the facebook manager for this activity when a
	 * posting action has been completed.
	 */
	public void notifyPostComplete() {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
	}

	/**
	 * @return True if connected to the internet, false otherwise.
	 */
	private boolean isOnline() {
		ConnectivityManager con = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
		NetworkInfo n = con.getActiveNetworkInfo();
		if (n != null)
			return n.isConnectedOrConnecting();
		else
			return false;
	}

}
